import formatTime from '../../utils/formatTime.js'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
var req = require("../../utils/action.js");
var utils = require("../../utils/util.js");

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: Object,
    ifHimself: {
      value: true,
      type: Boolean
    },
    userInfo:Object
  },
  options: {
    styleIsolation: 'apply-shared',
    multipleSlots: true,
  },
  observers: {
    ['item.createTime'](val) {
      if (val) {
        console.log(val)
        this.setData({
          _createTime: formatTime(new Date(val))
        })
      }
    },
    ['item.watchNum'](val) {
      this.setData({
        _watchNum: val? this.formatNum(val):'0'
      })
    },
    ['item.commentNum'](val) {
      this.setData({
        _commentNum: val? this.formatNum(val):'0'
      })
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    _createTime: '',
    _watchNum: '',
    _commentNum: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    formatNum(num) {
      if (num < 1000) {
        return num.toString()
      } else if (num<10000) {
        let numStr = num.toString()
        return numStr[0]+'.'+numStr[1] + 'k'
      } else {
        return '1w+'
      }
    },
    onPressDelete(e) {
      let _url = req.actions.ym+ "invitation/deleteInvitation",params={invitationId:e.currentTarget.dataset.id,customerId:this.data.userInfo.id},that=this
      Dialog.confirm({
        title: '确认要删除此贴子吗',
      }).then(() => {
        utils.getAction(params,_url,function(res){
          console.log("deleteInvitation===>"+JSON.stringify(res))
          if(res.data.code == 200){
            that.setData({'item.del_status':'1'})
            wx.showToast({title: '删除成功！'})
            wx.switchTab({url: '/pages/campus/campus'})
          }else{
            Dialog.alert({message: res.data.message})
          }
        })
      })
    },
    onPressOver(e) {
      let _url = req.actions.ym+ "invitation/overInvitation",params={invitationId:e.currentTarget.dataset.id,customerId:this.data.userInfo.id},that=this
      //this.triggerEvent('onPressOver')
      Dialog.confirm({
        title: '确定要结束此贴吗？',
      }).then(() => {
          utils.getAction(params,_url,function(res){
            console.log("overInvitation===>"+JSON.stringify(res))
            if(res.data.code == 200){
              that.setData({'item.over_status':'1'})
              wx.showToast({title: '结贴成功！'})
            }else{
              Dialog.alert({message: res.data.message})
            }
          })
      })
    },
    onPreviewImage(event) {
      wx.previewImage({
        urls: this.data.item.imgList,
        current: event.target.dataset.imgsrc
      })
    },
  },
})

